package org.mv.mm.common.options;

import javax.swing.JSpinner;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import org.openide.util.NbPreferences;

final class SavingPanel extends javax.swing.JPanel implements DocumentListener {

  private final SavingOptionsPanelController controller;

  SavingPanel(SavingOptionsPanelController controller) {
    this.controller = controller;
    initComponents();
    // TODO listen to changes in form fields and call controller.changed()
    JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor) minSavingInterval.getEditor();
    editor.getTextField().getDocument().addDocumentListener(this);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        minSavingInterval = new javax.swing.JSpinner();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(SavingPanel.class, "SavingPanel.jLabel1.text")); // NOI18N
        jLabel1.setToolTipText(org.openide.util.NbBundle.getMessage(SavingPanel.class, "SavingPanel.jLabel1.toolTipText")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(SavingPanel.class, "SavingPanel.jLabel2.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(minSavingInterval, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(minSavingInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(182, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

  void load() {
    // TODO read settings and initialize GUI
    // Example:        
    // someCheckBox.setSelected(Preferences.userNodeForPackage(SavingPanel.class).getBoolean("someFlag", false));
    // or for org.openide.util with API spec. version >= 7.4:
    // someCheckBox.setSelected(NbPreferences.forModule(SavingPanel.class).getBoolean("someFlag", false));
    
    // Load the interval in miliseconds and convert into seconds to present in the dialog.
    minSavingInterval.setValue( NbPreferences.root().getInt("minSavingInterval", 60000)/60/1000 );
  }

  void store() {
    // TODO store modified settings
    // Example:
    // Preferences.userNodeForPackage(SavingPanel.class).putBoolean("someFlag", someCheckBox.isSelected());
    // or for org.openide.util with API spec. version >= 7.4:
    // NbPreferences.forModule(SavingPanel.class).putBoolean("someFlag", someCheckBox.isSelected());

    // Save the interval in miliseconds instead of seconds presented in the dialog.
    Integer i = (Integer) minSavingInterval.getValue();
    NbPreferences.root().putInt("minSavingInterval",i*1000*60 );
  }

  boolean valid() {
    // TODO check whether form is consistent and complete
//TODO přidat kontrolu čísel, a vůbec omezit jen na čísla...
    if (minSavingInterval.getValue() == null) {
      return false;
    } else {
      return true;
    }

  }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSpinner minSavingInterval;
    // End of variables declaration//GEN-END:variables

  @Override
  public void insertUpdate(DocumentEvent e) {
    controller.changed();
  }

  @Override
  public void removeUpdate(DocumentEvent e) {
    controller.changed();
  }

  @Override
  public void changedUpdate(DocumentEvent e) {
    controller.changed();
  }
}
